/**
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 */
package com.ce.pms.house.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.annotation.AssociatedBuildingQuery;
import com.ce.pms.common.annotation.BuildingQuery;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.PmsUtilityComparator;
import com.ce.pms.common.util.UpLoadFile;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IAreaDao;
import com.ce.pms.house.dao.IBuildingDao;
import com.ce.pms.house.dao.IChargePrecinctDao;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.dao.IPrecinctDao;
import com.ce.pms.house.dao.IProjectDao;
import com.ce.pms.house.model.AreaDomain;
import com.ce.pms.house.model.BuildingDomain;
import com.ce.pms.house.model.HouseGrp;
import com.ce.pms.house.model.ImageDomain;
import com.ce.pms.house.model.ProjectDomain;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IImageService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.HouseGrpVO;
import com.ce.pms.house.vo.ImageVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

/**
 * 功能描述: BuildingServiceImpl,楼栋资料维护业务类
 *
 * @author  付湘辉
 * Created on：  2008-6-25
 * @author
 * update on：
 */
public class BuildingServiceImpl extends PmsBaseServiceImpl implements
		IBuildingService {

	/**
	 * 图片服务
	 */
	protected IImageService imageService;

	/**
	 * 楼盘Dao注入
	 */
	private IAreaDao areaDao;

	/**
	 * 楼栋Dao
	 */
	private IBuildingDao buildingDao;

	/**
	 * 房屋Dao
	 */
	private IHouseDao houseDao;

	/**
	 * 收费管理区Dao
	 */
	private IChargePrecinctDao chargePrecinctDao;

	/**
	 * 期数Dao注入
	 */
	private IProjectDao projectDao;

	/**
	 *
	 * 功能描述: 新增楼栋信息
	 *
	 * @author 付湘辉
	 * Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param buildingVo
	 * @return BuildingVO 返回结果
	 */
	public BuildingVO addBuilding(BuildingVO buildingVo){

		try{
			super.checkParameterIsNull(buildingVo, "common:parameters_null_exception, ");
			buildingVo.checkRequiredField();
			if (this.checkSeqNoIsExist(buildingVo.getProjectId(), buildingVo.getSeqNo())) {
				throw new BusinessException("house:building_seqno_exist_exception",BusinessException.WARNING);
			}

			if (this.checkBuildingNameIsExist(buildingVo.getAreaId(), buildingVo.getName())) {
				throw new BusinessException("house:building_name_exist_exception",BusinessException.WARNING);
			}

			//组装楼栋实体对象
			BuildingDomain buildingDo = this.assembleV2D(buildingVo);
			//检查楼层约束
			buildingDo.checkStoreyRole();
			//处理楼栋单元
			buildingDo.generationHouseGrp(buildingVo.getCellCount());

			this.buildingDao.saveObject(buildingDo);

			buildingVo.setKeyword(buildingDo.getKeyword());
		} catch (BusinessException be) {
			this.processIfProduceException(buildingVo);
			throw be;
		} catch (Exception e) {
			this.processIfProduceException(buildingVo);
			super.logger.error(e.getMessage(), e);
			throw new BusinessException("house:building_save_exception",BusinessException.FAILURE);
		}
		return buildingVo;
	}

	/**
	 *
	 * 功能描述: 从一个BuildingDomain对象装配一个BuildingVO对象
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：
	 * @param buildingDomain
	 * @return BuildingVO 返回结果
	 */
	public BuildingVO assembleD2V(BuildingDomain buildingDomain){
		BuildingVO buildVO = new BuildingVO();

		super.checkParameterIsNull(buildingDomain, "common:parameters_null_exception, ");
		//进行对拷
		BeanUtil.convertVoAsDo(buildingDomain, buildVO);

		buildVO.setAreaId(buildingDomain.getArea() == null ? null
				: buildingDomain.getArea().getKeyword());// 楼盘id
		buildVO.setAreaName(buildingDomain.getArea() == null ? ""
				: buildingDomain.getArea().getName());// 楼盘名称
		buildVO.setProjectId(buildingDomain.getProject() == null ? null
				: buildingDomain.getProject().getKeyword());// 建设期id
		buildVO.setProjectName(buildingDomain.getProject() == null ? ""
				: buildingDomain.getProject().getName());// 建设期名称

		buildVO.setAerialDefenceLevelId(buildingDomain.getAerialDefenceLevel() == null ? null
						: buildingDomain.getAerialDefenceLevel().getId());// 设置人防等级id
		buildVO.setAerialDefenceLevelName(buildingDomain.getAerialDefenceLevel() == null ? ""
						: buildingDomain.getAerialDefenceLevel().getName());// 设置人防等级name
		buildVO.setAseismeticLevelId(buildingDomain.getAseismeticLevel() == null ? null
						: buildingDomain.getAseismeticLevel().getId());// 设置抗震等级id
		buildVO.setAseismeticLevelName(buildingDomain.getAseismeticLevel() == null ? ""
						: buildingDomain.getAseismeticLevel().getName());// 设置抗震等级name
		buildVO.setFireproofLevelId(buildingDomain.getFireproofLevel() == null ? null
						: buildingDomain.getFireproofLevel().getId());// 设置防火等级id
		buildVO.setFireproofLevelName(buildingDomain.getFireproofLevel() == null ? ""
						: buildingDomain.getFireproofLevel().getName());//设置防火等级name

		//装配规划图
		buildVO.setLayoutImageVo(this.imageService.convertDomainToVo(buildingDomain.getLayoutImage()));

		//装配平面图集合
		buildVO.setIchnographys(this.assembleIchnographyColls(buildingDomain.getIchnographys()));

		return buildVO;
	}

	/**
	 * 功能描述: 验证指定名称的楼栋在指定楼盘下是否存在,要求id不是buildingId的楼栋 <br>
	 * @param areaId
	 * @param buildingId
	 * @param buildingName
	 * @return true 存在  false 不存在
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-16  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkBuildingNameIsExist(Long areaId,Long buildingId, String buildingName){
		super.checkParameterIsNull(areaId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(buildingId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(buildingName, "common:parameters_null_exception,seqNo");
		return this.buildingDao.getBuildingDomainBy(areaId, buildingId, buildingName)!=null;
	}

	/**
	 * 功能描述: 验证指定名称的楼栋在指定楼盘下是否存在 <br>
	 * @param areaId
	 * @param buildingName
	 * @return true 存在  false 不存在
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-16  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkBuildingNameIsExist(Long areaId, String buildingName){
		super.checkParameterIsNull(areaId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(buildingName, "common:parameters_null_exception,seqNo");
		return this.buildingDao.getBuildingDomainBy(areaId, buildingName)!=null;
	}

	/**
	 * 功能描述: 验证指定的楼栋编码在指定建设期下是否存在 <br>
	 * @param projectId
	 * @param seqNo
	 * @return true 存在  false 不存在
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-16  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkSeqNoIsExist(Long projectId, Integer seqNo){
		super.checkParameterIsNull(projectId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(seqNo, "common:parameters_null_exception,seqNo");
		return this.buildingDao.getSeqNoBy(projectId, seqNo)!=null;
	}

	/**
	 * 功能描述: 验证指定的楼栋编码在指定建设期下是否存在,要求id不是buildingId的楼栋 <br>
	 * @param projectId
	 * @param buildingId
	 * @param seqNo
	 * @return true 存在  false 不存在
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-16  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkSeqNoIsExist(Long projectId,Long buildingId, Integer seqNo){
		super.checkParameterIsNull(projectId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(buildingId, "common:parameters_null_exception,ID");
		super.checkParameterIsNull(seqNo, "common:parameters_null_exception,seqNo");
		return this.buildingDao.getSeqNoBy(projectId,buildingId,seqNo)!=null;
	}

	/**
	 * 功能描述: 根据楼栋id删除一个楼栋，如果楼栋下面已有房屋或楼栋已被管理区选用则不能删除 <br>
     *  imageList是一个从调用方传入的列表，它必须是已初始化的，<br>
     * 当这个楼栋有图片时，将放入这个列表，由调用者删除图片文件<br>
	 * @param buildingId
	 * @param imageList
     *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void deleteBuilding(Long buildingId,List<ImageVO> imageList){
		try {
            super.checkParameterIsNull(buildingId, "common:parameters_null_exception,ID");
			BuildingDomain building = this.buildingDao.findById(buildingId);

            this.checkBuildingDeleteRole(building, buildingId);

			building.getHouseGrps().clear();

			ImageVO tempImageVo = null;
			ImageDomain layoutImage =  building.getLayoutImage();
			if (!BlankUtil.isBlank(layoutImage)) {
				tempImageVo = new ImageVO();
				tempImageVo.setPath(layoutImage.getPath());
				tempImageVo.setName(layoutImage.getName());
				imageList.add(tempImageVo);
			}

			Set<ImageDomain> ichnographys = building.getIchnographys();
			if (!BlankUtil.isBlank(ichnographys)) {
				for (ImageDomain idomain : ichnographys) {
					tempImageVo = new ImageVO();
					tempImageVo.setPath(idomain.getPath());
					tempImageVo.setName(idomain.getName());
                    imageList.add(tempImageVo);
				}
			}

			this.doDelete(building);

		} catch(BusinessException be){
			throw be;
		} catch (Exception e){
			super.logger.error(e.getMessage(), e);
			throw new BusinessException("house:building_delete_exception",BusinessException.FAILURE);
		}
	}

	/**
	 *
	 * 功能描述: 修改楼栋信息
	 *
	 * @author 付湘辉
	 * Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param buildingVo
	 * @return BuildingVO 返回结果
	 */
	public BuildingVO editBuilding(BuildingVO buildingVo){

		try{
			super.checkParameterIsNull(buildingVo, "common:parameters_null_exception, ");
			buildingVo.checkRequiredField();
			if (this.checkSeqNoIsExist(buildingVo.getProjectId(),buildingVo.getKeyword(), buildingVo.getSeqNo())) {
				throw new BusinessException("house:building_seqno_exist_exception",BusinessException.WARNING);
			}

			if (this.checkBuildingNameIsExist(buildingVo.getAreaId(),buildingVo.getKeyword(), buildingVo.getName())) {
				throw new BusinessException("house:building_name_exist_exception",BusinessException.WARNING);
			}

			//组装楼栋实体对象
			BuildingDomain buildingDo = this.assembleV2D(buildingVo);
			//检查楼层约束
			buildingDo.checkStoreyRole();
			//处理楼栋单元
			buildingDo.generationHouseGrp(buildingVo.getCellCount());

			this.buildingDao.updateObject(buildingDo);

		} catch(BusinessException be){
			throw be;
		} catch (Exception e){
			this.processIfProduceException(buildingVo);
			super.logger.error(e.getMessage(), e);
			throw new BusinessException("house:building_edit_exception",BusinessException.FAILURE);
		}

		return buildingVo;
	}

    /**
	 *
	 * 功能描述: 生成一个指定楼盘期数下的新的楼栋编码
	 *
	 * @author 付湘辉
	 * Created on:2008-7-9
	 *
	 * 修改原因：
	 * @param projectId
	 * @return Integer 返回结果
	 */
	public Integer generationMaxSeqNoByProjectId(Long projectId){

		super.checkParameterIsNull(projectId, "common:parameters_null_exception, ");
		Integer maxSeqNo = null;
		//得到楼栋编码最大值
		maxSeqNo = this.buildingDao.getMaxSeqNoByProjectId(projectId);

		//加１作为新的楼栋编码
		return maxSeqNo + 1;
	}

	/**
	 *
	 * 功能描述: 根据楼栋Id得到楼栋单元列表,如果指定的楼栋不分单元，返回一个不含数据的列表
	 *
	 * @author 付湘辉
	 * Created on:2008-6-25
	 *
	 * 修改原因：
	 * @param buildingId
	 * @return List<HouseGrp> 返回结果
	 */
	public List<HouseGrp> getBuildingCellList(Long buildingId) {
		super.checkParameterIsNull(buildingId, "common:parameters_null_exception, ");
		return this.buildingDao.getBuildingCells(buildingId);
	}

	/**
	 * 功能描述: 查询所有楼栋的列表，同时查询其中的单元信息 <br>
	 * @return List<BuildingVO>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@BuildingQuery(alias = "building_V", beginStr = " WHERE ", endStr = " ", insertAfterPoint = "building_V", property4BuildingId = "keyword")
	public List<BuildingVO> getAllBuildingList(){
		List<BuildingVO> resultList = new ArrayList<BuildingVO>();
		List<BuildingDomain> buildDoList = this.buildingDao.findAllResult("from BuildingDomain building_V", null);
		if (!BlankUtil.isBlank(buildDoList)) {
			for (BuildingDomain domain : buildDoList) {
				BuildingVO tempBuildingVO = this.assembleD2V(domain);//装配楼栋vo
				Set<HouseGrpVO> tempSet = new HashSet<HouseGrpVO>();

				Set<HouseGrp> tempHouseGrps = domain.getHouseGrps();

				for (HouseGrp grp : tempHouseGrps) {
					HouseGrpVO tempHouseGrpVO = this.assembleD2V(grp);//装配楼栋单元vo
					tempSet.add(tempHouseGrpVO);
				}

				tempBuildingVO.setHouseGrpVos(tempSet);//楼栋单元集合
				resultList.add(tempBuildingVO);
			}
		}
		Collections.sort(resultList, PmsUtilityComparator.getComparator());
		return resultList;
	}

	/**
	 *
	 * 功能描述: 根据楼盘id查询其所有楼栋，需要将楼栋下的单元查询出来。
	 *
	 * @author 付湘辉
	 * Created on:2008-7-4
	 *
	 * 修改原因：
	 * @param areaId
	 * @return List<BuildingVO> 返回结果
	 */
	public List<BuildingVO> getBuildingListByAreaId(Long areaId){
		super.checkParameterIsNull(areaId, "common:parameters_null_exception, ");
		List<BuildingVO> resultList = new ArrayList<BuildingVO>();
		List<BuildingDomain> buildDoList = null;
		BuildingVO tempBuildingVO = null;
		HouseGrpVO tempHouseGrpVO = null;
		Set<HouseGrpVO> tempSet = null;

		//根据楼盘id查询其所有楼栋信息
		buildDoList = this.buildingDao.getBuildingListByAreaId(areaId);

		if (!BlankUtil.isBlank(buildDoList)) {
			for (BuildingDomain domain : buildDoList) {
				tempBuildingVO = this.assembleD2V(domain);//装配楼栋vo
				tempSet = new HashSet<HouseGrpVO>();

				Set<HouseGrp> tempHouseGrps = domain.getHouseGrps();

				for (HouseGrp grp : tempHouseGrps) {
					tempHouseGrpVO = this.assembleD2V(grp);//装配楼栋单元vo
					tempSet.add(tempHouseGrpVO);
				}

				tempBuildingVO.setHouseGrpVos(tempSet);//楼栋单元集合
				resultList.add(tempBuildingVO);
			}
		}

		return resultList;
	}

	/**
	 *
	 * 功能描述: 根据楼盘id查询其所有楼栋实体。
	 *
	 * @author 付湘辉
	 * Created on:2008-10-23
	 *
	 * 修改原因：
	 * @param areaId
	 * @return List<BuildingDomain> 返回结果
	 */
	public List<BuildingDomain> getBuildingDoListByAreaId(Long areaId){
		super.checkParameterIsNull(areaId, "common:parameters_null_exception, ");
		List<BuildingDomain> buildDoList = new ArrayList<BuildingDomain>();
		//根据楼盘id查询其所有楼栋信息
		buildDoList = this.buildingDao.getBuildingListByAreaId(areaId);

		return buildDoList;
	}

	/**
	 * 功能描述: 根据楼栋id获得一个楼栋的信息 <br>
	 * @param buildingId
	 * @return BuildingVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-17  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BuildingVO getBuildingVOById(Long buildingId){
		super.checkParameterIsNull(buildingId, "common:parameters_null_exception, ");
		BuildingDomain buildingDomain = this.buildingDao.findById(buildingId);
		BuildingVO buildVO = this.assembleD2V(buildingDomain);

		//住宅套数
		Integer resiNumber = this.calculateResiNumber(buildingDomain.getKeyword());
		buildVO.setResiNumber(resiNumber);

		//楼栋单元数
		buildVO.setCellCount(buildingDomain.getHouseGrps().size());

		if (this.houseDao.getHouseCount(buildingId) > 0) {
			buildVO.setPermitModifyCell(false);
		}else{
			buildVO.setPermitModifyCell(true);
		}

		return buildVO;
	}

	/**
	 *
	 * 功能描述: 获取耐火等级列表，使用SysrefcodeDO对象封装数据
	 *
	 * @author 付湘辉
	 * Created on:2008-6-30
	 *
	 * 修改原因：
	 * @return List<SysrefcodeDO> 返回结果
	 */
	public List<SysrefcodeDO> getFireproofingLevelList(){
		List<SysrefcodeDO> list = new ArrayList<SysrefcodeDO>();

		list = super.sysrefcodeDao.querySysrefCodesByCode(SysrefcodeDO.HM_BUILDING_FIREPROOFING_LEVEL_CODE);

		return list;
	}

	/**
	 *
	 * 功能描述: 获取楼栋住宅层楼层阿拉伯数字列表
	 *
	 * @author 付湘辉
	 * Created on:2008-6-30
	 *
	 * 修改原因：
	 * @param buildingId 楼栋id
	 * @return List<Integer> 返回结果
	 */
	public List<Integer> getHouseStoreyList(Long buildingId){
		super.checkParameterIsNull(buildingId, "common:parameters_null_exception, ");

		return this.buildingDao.getHouseStoreyList(buildingId);
	}

	/**
	 *
	 * 功能描述: 获取人防等级列表，使用SysrefcodeDO对象封装数据
	 *
	 * @author 付湘辉
	 * Created on:2008-6-30
	 *
	 * 修改原因：
	 * @return List<SysrefcodeDO> 返回结果
	 */
	public List<SysrefcodeDO> getPersonSafeguardList(){
		List<SysrefcodeDO> list = new ArrayList<SysrefcodeDO>();

		list = super.sysrefcodeDao.querySysrefCodesByCode(SysrefcodeDO.HM_BUILDING_PERSON_SAFEGUARD_LEVEL_CODE);

		return list;
	}

	/**
	 *
	 * 功能描述: 获取抗震等级列表，使用SysrefcodeDO对象封装数据
	 *
	 * @author 付湘辉
	 * Created on:2008-6-30
	 *
	 * 修改原因：
	 * @return List<SysrefcodeDO> 返回结果
	 */
	public List<SysrefcodeDO> getQuakeproofLevelList(){
		List<SysrefcodeDO> list = new ArrayList<SysrefcodeDO>();

		list = super.sysrefcodeDao.querySysrefCodesByCode(SysrefcodeDO.HM_BUILDING_QUAKEPROOF_LEVEL_CODE);

		return list;
	}

	/**
	 * 功能描述: 设置楼盘Dao
	 *
	 * @author 付湘辉
	 * Created on:2008-7-7
	 *
	 * 修改原因：
	 * @param areaDao the 楼盘Dao to set
	 */
	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	/**
	 * 功能描述: 设置楼栋Dao
	 *
	 * @author 付湘辉
	 * Created on:2008-6-27
	 *
	 * 修改原因：
	 * @param buildingDao the 楼栋Dao to set
	 */
	public void setBuildingDao(IBuildingDao buildingDao) {
		this.buildingDao = buildingDao;
	}


	/**
	 * @param houseDao the houseDao to set
	 */
	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	/**
	 * 功能描述: 设置图片服务
	 *
	 * @author 付湘辉
	 * Created on:2008-7-1
	 *
	 * 修改原因：
	 * @param imageService the 图片服务 to set
	 */
	public void setImageService(IImageService imageService) {
		this.imageService = imageService;
	}


	/**
	 * 功能描述: 设置期数Dao
	 *
	 * @author 付湘辉
	 * Created on:2008-7-7
	 *
	 * 修改原因：
	 * @param projectDao the 期数Dao to set
	 */
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * 功能描述:验证住宅层是否已经有房间了
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-8-26
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String validationFloor(Long keyword, Integer homeFrom, Integer homeTo){
		List list = buildingDao.validationFloor(keyword , homeFrom , homeTo);
		if(list.size()>0){
			List<Integer> count=buildingDao.FloorMaxMin(keyword);
			String tempMessage="";
			if(count.get(1)==count.get(0)){
				tempMessage=tempMessage+count.get(0);
			} else {
				tempMessage=count.get(1)+"~"+count.get(0);
			}
			return tempMessage;
		}else{
			return "true";
		}

	}

	/**
	 * 功能描述: 根据收费管理区id查询其下的所有楼栋信息列表 <br>
	 * @param precinctId 收费管理区id
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BuildingVO> queryBuildListByPrecinctId(Long precinctId){
		List<BuildingVO> list = new ArrayList<BuildingVO>();
		List<BuildingDomain> doList = null;
		doList = this.buildingDao.getBuildingListByPrecinctId(precinctId);
		for (BuildingDomain domain : doList) {
			list.add(this.assembleD2V(domain));
		}

		Collections.sort(list, PmsUtilityComparator.getComparator());

		return list;
	}

	/**
	 *
	 * 功能描述: 从一个houseGrpDomain对象装配一个HouseGrpVO对象
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：
	 * @param houseGrpDomain
	 * @return HouseGrpVO 返回结果
	 */
	private HouseGrpVO assembleD2V(HouseGrp houseGrpDomain){
		HouseGrpVO houseGrpVO = new HouseGrpVO();
		super.checkParameterIsNull(houseGrpDomain, "common:parameters_null_exception, ");

		//对拷
		BeanUtil.convertVoAsDo(houseGrpDomain, houseGrpVO);
		houseGrpVO.setProjectId(houseGrpDomain.getBuilding().getProject().getKeyword());//建设期id
		houseGrpVO.setBuildingId(houseGrpDomain.getBuilding().getKeyword());//楼栋id
		houseGrpVO.setBuildingName(houseGrpDomain.getBuilding().getName());//楼栋名称

		return houseGrpVO;
	}

	/** 功能描述: 装配平面图集合 <br>
	 * @param ichnographys
	 * @return Set<ImageVO>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-17  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private Set<ImageVO> assembleIchnographyColls(Set<ImageDomain> ichnographys) {

		if (BlankUtil.isBlank(ichnographys)) {
			return new HashSet<ImageVO>();
		}

		Set<ImageVO> ichnographyVOs = new HashSet<ImageVO>();
		ImageVO ichnographyVo = null;

		for (ImageDomain domain : ichnographys) {
			ichnographyVo = this.imageService.convertDomainToVo(domain);
			ichnographyVOs.add(ichnographyVo);
		}

		return ichnographyVOs;
	}

	/**
	 * 功能描述: 装配一个楼栋VO对象到楼栋DO
	 *
	 * @author 付湘辉
	 * Created on:2008-7-8
	 *
	 * 修改原因：
	 * @param buildingVo
	 * @return BuildingDomain 返回结果
	 */
	private BuildingDomain assembleV2D(BuildingVO buildingVo) {
		BuildingDomain buildingDo = null;
		if (BlankUtil.isBlank(buildingVo.getKeyword())) {
			buildingDo = new BuildingDomain();
			SysInfo sysInfo = super.createInfo(buildingVo.getCreator());
			buildingDo.setSysInfo(sysInfo);//创建信息
		}else{
			buildingDo = this.buildingDao.findById(buildingVo.getKeyword());
			super.modifyInfo(buildingDo.getSysInfo(), buildingVo.getCreator());
		}
		//进行对拷
		BeanUtil.convertVoAsDo(buildingVo, buildingDo);

		AreaDomain area = this.areaDao.findById(buildingVo.getAreaId());
		buildingDo.setArea(area);//楼盘

		ProjectDomain project = this.projectDao.getProject(buildingVo.getProjectId());
		buildingDo.setProject(project);//期数
		if (!BlankUtil.isBlank(buildingVo.getFireproofLevelId())) {
			SysrefcodeDO fireproofLevel = this.sysrefcodeDao.findById(buildingVo.getFireproofLevelId());
			buildingDo.setFireproofLevel(fireproofLevel);//防火等级
		}

		if (!BlankUtil.isBlank(buildingVo.getAseismeticLevelId())) {
			SysrefcodeDO aseismeticLevel = this.sysrefcodeDao.findById(buildingVo.getAseismeticLevelId());
			buildingDo.setAseismeticLevel(aseismeticLevel);//抗震等级
		}

		if (!BlankUtil.isBlank(buildingVo.getAerialDefenceLevelId())) {
			SysrefcodeDO aerialDefenceLevel = this.sysrefcodeDao.findById(buildingVo.getAerialDefenceLevelId());
			buildingDo.setAerialDefenceLevel(aerialDefenceLevel);//人防等级
		}

		//处理楼栋规划图
		this.processBuildingLayoutImage(buildingVo, buildingDo);
		//处理楼栋平面图
		buildingDo.getIchnographys().clear();
		buildingDo.getIchnographys().addAll(this.imageService.processImageSet(buildingVo.getIchnographys(), buildingVo.getCreator()));

		return buildingDo;
	}

	/**
     * 功能描述:  检查删除楼栋的业务规则 <br>
     *
     * @param buildingDo
     * @param buildingId
     *
     * Create author：付湘辉  <br>
     * Create on:2008-7-25  <br>
     * Edit author：  <br>
     * Edit on:    <br>
     * Why:     <br>
     */
    private void checkBuildingDeleteRole(BuildingDomain buildingDo, Long buildingId) {
        //已有房屋信信息时不能删除
        Integer houseCount = this.houseDao.getHouseCount(buildingId);
        if (SysrefcodeDO.CONSTANTS_ZERO < houseCount) {
        	throw new BusinessException("house:building_delete_house_exist_exception",
        								BusinessException.WARNING);
        }
        //已被管理区选用
        List<BuildingDomain> list = this.chargePrecinctDao.getBuildings();
        if (!BlankUtil.isBlank(list)) {
        	for (BuildingDomain domain : list) {
        		if (domain.getKeyword()==buildingDo.getKeyword()) {
        			throw new BusinessException("house:building_already_was_chosen_exception",
        					BusinessException.WARNING);
        		}
        	}
        }
    }

	/**
	 * 功能描述: 执行删除动作 <br>
	 * @param building
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-21  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private void doDelete(BuildingDomain building){
		this.buildingDao.deleteObject(building);
	}

	/** 功能描述: 得到一个可能的图片Domain <br>
	 * @param imageVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
//	private ImageDomain getPossibleImageDomain(ImageVO imageVo) {
//		ImageDomain imageDo = null;
//		if (!BlankUtil.isBlank(imageVo)) {
//			imageVo = this.processImage(imageVo);
//			if (!BlankUtil.isBlank(imageVo.getId())) {
//				imageDo = new ImageDomain();
//				imageDo.setKeyword(imageVo.getId());
//			}
//		}
//		return imageDo;
//	}

	/** 功能描述: 得到一个可能的图片Domain <br>
	 * @param imageVo
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
//	private ImageDomain getPossibleImageDomain(ImageVO imageVo,Long operator) {
//		if (!BlankUtil.isBlank(imageVo)) {
//			this.checkCreatorInfo(operator);
//			imageVo.setCreator(operator);
//			return this.getPossibleImageDomain(imageVo);
//		}
//		return null;
//	}

	/** 功能描述: 处理楼栋规划图的新增或修改 <br>
	 * @param buildingVo
	 * @param buildingDo
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void processBuildingLayoutImage(BuildingVO buildingVo, BuildingDomain buildingDo) {
		ImageVO imageVo = buildingVo.getLayoutImageVo();//规划图VO
		buildingDo.setLayoutImage(this.imageService.getPossibleImageDomain(imageVo,buildingVo.getCreator()));//规划图
	}

	/** 功能描述:  处理添加或修改楼栋时发生异常则删除已上传的图片<br>
	 * @param buildingVo
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	*/
	private void processIfProduceException(BuildingVO buildingVo) {
		if (!BlankUtil.isBlank(buildingVo.getLayoutImageVo())) {
			UpLoadFile.deleteFile(buildingVo.getLayoutImageVo());
		}
		if (!BlankUtil.isBlank(buildingVo.getIchnographys())) {
			Set<ImageVO> ichos = buildingVo.getIchnographys();
			for (ImageVO imageVO : ichos) {
				UpLoadFile.deleteFile(imageVO);
			}
		}
	}

	/**
	 * 功能描述:  根据传入的参数值对图片进行操作，规则如下： <br>
	 * <ul>
	 * <li>如果imageVo为空，直接返回。
	 * <li>如果imageVo的名称和路径值不为空值，但没有id，则进行新增操作
	 * <li>如果imageVo的名称和路径值不为空值，但有id，则进行修改操作
	 * <li>如果imageVo只有id，进行删除操作（包括删除图片文件和数据库表记录）
	 * </ul>
	 *
	 * @param imageVo
	 * @return ImageVO
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
//	private ImageVO processImage(ImageVO imageVo){
//		if (BlankUtil.isBlank(imageVo)) {
//			return null;
//		}
//		ImageVO resultImage = imageVo;
//		if (!BlankUtil.isBlank(resultImage.getName())
//			&& !BlankUtil.isBlank(resultImage.getPath())) {
//			Long imageId = resultImage.getId();
//			if (!BlankUtil.isBlank(imageId)) {//修改操作
//				ImageVO tempImage = this.imageService.findImageByID(imageId);
//				//图片名称不一样，进行修改
//				if (!resultImage.getName().equals(tempImage.getName())) {
//					UpLoadFile.deleteFile(tempImage.getPath(), tempImage.getName());
//					this.imageService.editImage(resultImage);
//				}
//			}else{
//				//添加图片
//				imageId = this.imageService.addImage(resultImage);
//
//				//得到新增的图片id
//				resultImage.setId(imageId);
//			}
//		}else if(!BlankUtil.isBlank(resultImage.getId())){//只传id，删除文件
//			ImageVO tempImageVo = this.imageService.findImageByID(resultImage.getId());
//			this.imageService.deleteImageByID(tempImageVo.getId());//从数据库删除图片
//			UpLoadFile.deleteFile(tempImageVo.getPath(), tempImageVo.getName());
//			resultImage.setId(null);
//		}
//		return resultImage;
//	}

	/**
	 * 功能描述:  <br>
	 * @param imageVos
	 * @param operator
	 * @return Set<ImageDomain>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
//	private Set<ImageDomain> processImageSet(Set<ImageVO> imageVos,Long operator){
//		Set<ImageDomain> ichnographyDos =  new HashSet<ImageDomain>();
//		if (!BlankUtil.isBlank(imageVos)) {
//			for (ImageVO imageVO : imageVos) {
//				ichnographyDos.add(this.getPossibleImageDomain(imageVO,operator));
//			}
//			ichnographyDos.remove(null);
//		}
//
//		return ichnographyDos;
//	}

	/**
	 * 功能描述: 得到楼栋中住宅套数 <br>
	 * @param buildingId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-7-17  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Integer calculateResiNumber(Long buildingId){
		if (BlankUtil.isBlank(buildingId)) {
			return null;
		}
		return this.houseDao.getResiNumber(buildingId);
	}

	/**
	 * @param chargePrecinctDao the chargePrecinctDao to set
	 */
	public void setChargePrecinctDao(IChargePrecinctDao chargePrecinctDao) {
		this.chargePrecinctDao = chargePrecinctDao;
	}
}
